<?php

/**
 *    默认控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class PetApp extends BackendApp
{
	/**
	 *    后台首页
	 *
	 *    @author    Garbin
	 *    @return    void
	 */
	function index()
	{
		$db=&db();
		$conditions = '';

		$user_name=empty($_GET['user_name'])?'':$_GET['user_name'];
		$name=empty($_GET['name'])?'':$_GET['name'];

		$sex=empty($_GET['sex'])?'':$_GET['sex'];
		$cate_id=empty($_GET['cate_id'])?'':$_GET['cate_id'];

		if($user_name){
			$conditions.=" and (m.user_name like '%".$user_name."%' or m.real_name like '%".$user_name."%')";
		}
		if($name){
			$conditions.=" and name like '".$real_name."'";
		}
		if($sex){
			$conditions.=" and p.sex ='".$sex."'";
		}
		
		if($cate_id){
			$conditions.=" and exists(select 1 from cw_pcategory c 
LEFT JOIN cw_pcategory c1 on c.parent_id=c1.cate_id
where (c.cate_id=$cate_id or c1.cate_id=$cate_id) and c.cate_id=p.cate_id)";
		}
		

		$page   =   $this->_get_page(10);   //获取分页信息
		$sql_count = "SELECT COUNT(*) FROM cw_pet p
					LEFT JOIN cw_member m ON p.uid = m.user_id 
					WHERE 1=1 ".$conditions;
		$res_count = $db->getOne($sql_count);

		$sql = "SELECT p.*,m.user_name,m.real_name FROM cw_pet p
					LEFT JOIN cw_member m ON p.uid = m.user_id
				WHERE 1=1 ".$conditions." ORDER BY add_time desc LIMIT ".$page['limit'];

		$pets = $db->getAll($sql);
		$page['item_count']=$res_count;   //获取统计数据
		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条

		$list1=array();
		$sql="select d_key,d_value from cw_dictionary where d_code='petsex' order by d_order";
		$d=$db->getAll($sql);
		if($d){
			foreach ($d as $k=>$v){
				$list1[$v['d_key']]=$v['d_value'];
			}
		}

		$list2=array();
		$sql="select cate_id,cate_name from cw_pcategory where if_show=1 and parent_id=0 order by sort_order ";
		$c=$db->getAll($sql);
		if($c){
			foreach ($c as $k=>$v){
				$list2[$v['cate_id']]=$v['cate_name'];
			}
		}

		$this->assign('list1', $list1);
		$this->assign('list2', $list2);
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$this->assign('pets',$pets);
		$this->display('pet.index.html');
	}
}
?>
